/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

/**
 *
 * @author plato
 */
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import models.Student;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

class ModifyXMLFileCheck{

    public static void main(String argv[]) {

        try {
            String filepath = "d:\\file.xml";
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(filepath);
            String firstName = "ggg";
            String lastName = "ggh";
            String address = "hhh";
            String age = "12";
            String note = "note";
            Date dob = new Date(1998, 3, 12);
            Student objStudent = new Student(firstName, lastName, address, Integer.parseInt(age), dob.toString(), note, 1);
            // Get the root element
            Node students = doc.getFirstChild();

            // Get the staff element , it may not working if tag has spaces, or
            // whatever weird characters in front...it's better to use
            // getElementsByTagName() to get it directly.
            // Node staff = company.getFirstChild();

            // Get the staff element by tag name directly
            //Node staff = doc.getElementsByTagName("staff").item(0);
            Element student = doc.createElement("student");
            students.appendChild(student);
            Element firstNameE = doc.createElement("firstname");
            firstNameE.appendChild(doc.createTextNode(objStudent.getFirstName()));
            Element lastNameE = doc.createElement("lastname");
            lastNameE.appendChild(doc.createTextNode(objStudent.getLastName()));
            student.appendChild(firstNameE);
            student.appendChild(lastNameE);
            


            // loop the staff child node
//            NodeList list = staff.getChildNodes();
//
//            for (int i = 0; i < list.getLength(); i++) {
//
//                Node node = list.item(i);
//
//                // get the salary element, and update the value
//                if ("salary".equals(node.getNodeName())) {
//                    node.setTextContent("2000000");
//                }
//
//                //remove firstname
//                if ("firstname".equals(node.getNodeName())) {
//                    staff.removeChild(node);
//                }
//
//            }

            // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            Properties properties = new Properties();
            properties.setProperty(OutputKeys.INDENT, "yes");
            properties.setProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
            properties.setProperty(OutputKeys.METHOD, "xml");
            properties.setProperty(OutputKeys.VERSION, "1.0");
            properties.setProperty(OutputKeys.ENCODING, "UTF-8");
            properties.setProperty("{http://xml.apache.org/xslt}indent-amount", "4");

            transformer.setOutputProperties(properties);
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new File(filepath));
            transformer.transform(source, result);

            System.out.println("Done");

        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (TransformerException tfe) {
            tfe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (SAXException sae) {
            sae.printStackTrace();
        }
    }
}